package com.example.demo.service;

import com.example.demo.model.User;
import com.example.demo.rediesPorstroy.RediesPostroy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by DELL on 2018/12/27.
 */
@Service
public class MaterialImpl implements Materialservice {

    @Autowired
    private RediesPostroy rediesPostroy;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<User> queryall() {

        String redieskey= "queryuser";
//        List<User> users=rediesPostroy.queryall();
//       redisTemplate.opsForValue().set("queryuser",users);
        List<User> queryuser=(List<User>) redisTemplate.opsForValue().get("redieskey");
        //
        if(queryuser==null){
            List<User> queryusers=rediesPostroy.queryall();
            redisTemplate.opsForValue().set(redieskey,queryuser);
        }

        return queryuser;
    }



    @Override
    public User queryUser(Integer id) {

        String userkey="queryuser"+id;

        User user=rediesPostroy.querybyid(id);

        redisTemplate.opsForValue().set(userkey,user);

        return user;
    }

    @Override
    public int update(User user) {
        String userkey="queryuser"+ user.getId();

        rediesPostroy.update(user);
        User use=rediesPostroy.querybyid(user.getId());

        redisTemplate.opsForValue().set(userkey,use);

        if(use !=null){
            return 1;
        }
       return 0;
    }



}
